package com.hongkzh.www.adv.service;

import com.google.common.collect.Lists;
import com.hongkzh.www.conmon.IdGen;
import com.hongkzh.www.conmon.RedisTool;
import com.hongkzh.www.conmon.StaticPublic;
import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author martins
 */
public class AdvService extends BaseService {
    private static String imgSrc;


    public Page<Record> getRecommendList(int pageNumber) {
        SqlPara sqlPara = Db.getSqlPara("adv.getRecommendList");
        Page<Record> records = Db.paginate(pageNumber,StaticPublic.PAGESIZE,sqlPara);

        for (Record r : records.getList()) {
            doFilePath(r);

        }
        return records;

    }

    public List<Record> getStaticList() {
        SqlPara sqlPara = Db.getSqlPara("adv.getStaticList");
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = Lists.newArrayList();
        }
        for (Record r : records) {
            doFilePath(r);

        }
        return records;

    }

    public List<Record> getCityAdvList() {
        SqlPara sqlPara = Db.getSqlPara("adv.getCityAdvList");
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = Lists.newArrayList();
        }

        SqlPara categorySql = Db.getSqlPara("adv.getCityTagList");
        List<Record> categorys = Db.find(categorySql);
        if (categorys == null) {
            categorys = Lists.newArrayList();
        }

        String[] tags;

        for (Record r : records) {
            r.set("tags", categorys);
            List<Record> categorysd = Lists.newArrayList();
            if (StrKit.notBlank(r.getStr("tag"))) {
                tags = r.getStr("tag").split(",");
                for (String tag : tags
                        ) {
                    for (Record c : categorys
                            ) {
                        if (tag.equals(c.getStr("did"))) {
                            categorysd.add(c);
                            break;
                        }
                    }

                }
            }
            if (categorysd.size() > 0) {
                r.set("tags", categorysd);
            }
            doFilePath(r);

        }

        return records;

    }

    public List<Record> getCityList() {
        SqlPara sqlPara = Db.getSqlPara("adv.getCityList");
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = Lists.newArrayList();
        }
        for (Record r : records) {
            doFilePath(r);
        }
        return records;

    }

    public List<Record> getMediaList() {
        SqlPara sqlPara = Db.getSqlPara("adv.getMediaList");
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = Lists.newArrayList();
        }
        for (Record r : records) {
            doFilePath(r);
        }
        return records;

    }

    public List<Record> getEnterpriseList() {
        SqlPara sqlPara = Db.getSqlPara("adv.getEnterpriseList");
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = Lists.newArrayList();
        }
        for (Record r : records) {

            doFilePath(r);
        }
        return records;

    }

    public List<Record> getTraditionList() {
        SqlPara sqlPara = Db.getSqlPara("adv.getTraditionList");
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = Lists.newArrayList();
        }
        for (Record r : records) {
            doFilePath(r);
        }
        return records;

    }

    public Page<Record> getHotAdvList(int pageNumber) {
        SqlPara sqlPara = Db.getSqlPara("adv.getHotAdvList");
        Page<Record> records = Db.paginate(pageNumber, StaticPublic.PAGESIZE, sqlPara);

        for (Record r : records.getList()) {
            doFilePath(r);
        }
        return records;

    }

    public Record getAdvById(String id,String userId) {
        SqlPara sqlPara = Db.getSqlPara("adv.getAdvById", id,userId);
        Record r = Db.findFirst(sqlPara);
        if (r != null) {
            r.set("collectionState","0");
            if(StrKit.notBlank(r.getStr("collectionId"))){
                r.set("collectionState","1");
            }
            r.set("praiseState","0");
            if(StrKit.notBlank(r.getStr("praiseId"))){
                r.set("praiseState","1");
            }
            Record in= getAdvIntegralById(id,userId);
            r.set("integral",0);
            if(null!=in){
                r.set("integral",in.getInt("integral"));
            }
            doFilePath(r);
        }

        return r;

    }

    public  Record getAdvIntegralById(String id,String userId){
        SqlPara sqlPara = Db.getSqlPara("adv.getAdvIntegralById", id,userId);
        Record r = Db.findFirst(sqlPara);
        return r;
    }

    public Page<Record> getAdvCommentsById(String id, int pageNumber) {
        SqlPara sqlPara = Db.getSqlPara("adv.getAdvCommentsById", id);
        Page<Record> records = Db.paginate(pageNumber, 10, sqlPara);

        for (Record r : records.getList()) {
            doFilePath(r);
        }
        return records;

    }



    public List<Record> getAdvRecommendByAdvId(String id) {
        SqlPara sqlPara = Db.getSqlPara("adv.getAdvRecommendByAdvId",id,"2");
        List<Record> records = Db.find(sqlPara);
        for (Record r : records) {
            doFilePath(r);
        }
        return records;
    }

    public Map<String,List<Record>> getChinaList() {
        Map<String,List<Record>> map=new HashMap<>();
        List<Record> provinces=null,cities=null,areas=null;
        SqlPara sqlPara =null;
        if(!RedisTool.exists(StaticPublic.PROVINCES)) {
            sqlPara = Db.getSqlPara("adv.getProvinces");
           provinces = Db.find(sqlPara);
            if(null!=provinces&&provinces.size()>0) {
                RedisTool.setexObject(StaticPublic.PROVINCES,StaticPublic.COUNTRYTIME, (provinces));
            }
       }
       else{
           provinces= ( List<Record>)RedisTool.getObject(StaticPublic.PROVINCES);
       }
        map.put("provinces",provinces);

        if(!RedisTool.exists(StaticPublic.CITIES)) {
            sqlPara = Db.getSqlPara("adv.getCities");
             cities = Db.find(sqlPara);
            if(null!=cities&&cities.size()>0) {
                RedisTool.setexObject(StaticPublic.CITIES,StaticPublic.COUNTRYTIME, (cities));
            }
        }
        else{
            cities= ( List<Record>)RedisTool.getObject(StaticPublic.CITIES);
        }


        map.put("cities",cities);

        if(!RedisTool.exists(StaticPublic.AREAS)) {
            sqlPara = Db.getSqlPara("adv.getAreas");
            areas = Db.find(sqlPara);
            if(null!=areas&&areas.size()>0) {
                RedisTool.setexObject(StaticPublic.AREAS,StaticPublic.COUNTRYTIME, (areas));
            }
        }
        else{
            areas= ( List<Record>)RedisTool.getObject(StaticPublic.AREAS);
        }

        map.put("areas",areas);

        return map;
    }

    public List<Record> getCountryList() {
        SqlPara sqlPara =null;
        List<Record> countrys=null;
        if(!RedisTool.exists(StaticPublic.ALLCOUNTRY)) {
            sqlPara = Db.getSqlPara("adv.getCountryList");
            countrys = Db.find(sqlPara);
            if(null!=countrys&&countrys.size()>0) {
                RedisTool.setexObject(StaticPublic.ALLCOUNTRY,StaticPublic.COUNTRYTIME, (countrys));
            }
        }
        else{
            countrys= ( List<Record>)RedisTool.getObject(StaticPublic.ALLCOUNTRY);
        }
        return  countrys;
    }
    public List<Record> getCountryStateList(String parentId) {
        String key=String.format(StaticPublic.COUNTRYSTATE, parentId);
        SqlPara sqlPara =null;
        List<Record> countrys=null;
        if(!RedisTool.exists(key)) {
            sqlPara = Db.getSqlPara("adv.getStateList",parentId);
            countrys = Db.find(sqlPara);
            if(null!=countrys&&countrys.size()>0) {
                RedisTool.setexObject(key,StaticPublic.COUNTRYTIME, (countrys));
            }
        }
        else{
            countrys= ( List<Record>)RedisTool.getObject(key);
        }
        return  countrys;
    }

    /**
     * 1用户历史记录
     * 2广告积分
     * 3用户积分
     *
     * @param loginUid
     * @param advId
     * @return
     */
    @Before(Tx.class)
    public Record endPlayEnterpriseAdvById(String loginUid, String advId) {
        Record re = new Record();
        re.set("msg", "");

        Record r = this.getAdvById(advId,loginUid);
        String cid = IdGen.uuid();
        String sqlPara = Db.getSql("user.historyAdd");
        //用户历史记录
        Db.update(sqlPara, cid, loginUid, r.getStr("imgSrc"), r.getStr("imgLinks"), r.getStr("imgNote"), r.getStr("imgRank"), r.getStr("model")
                , advId, r.getStr("title"), r.getStr("type"), r.getStr("coverImgSrc"));
        re.set("integral", 0);
        r = this.getAdvIntegralById(advId,loginUid);
        if (null == r) {
            return re;
        }
        int integra = r.getInt("integral");
        re.set("integral", integra);
        //用户积分

        Record userIntegralr = Db.findFirst(Db.getSqlPara("user.selectUserIntegral", loginUid));
        int userIntegral = userIntegralr.getInt("integral");
        Db.update(Db.getSqlPara("user.updateUserIntegral", integra, loginUid));
        sqlPara = Db.getSql("user.insertUserIntegral");
        cid = IdGen.uuid();
        Db.update(sqlPara, cid, loginUid, integra, userIntegral + integra);

        //广告积分
        cid = IdGen.uuid();
        sqlPara = Db.getSql("enterprise.insertAdvIntegral");
        Db.update(sqlPara, cid, loginUid, integra, advId);

            cid = IdGen.uuid();

            Db.update(Db.getSqlPara("enterprise.insertAdvData", cid, integra, advId));



        return re;
    }

    public Record historyEnterpriseAdv(String id, String loginUid) {
        SqlPara sqlPara = Db.getSqlPara("adv.historyEnterpriseAdv",id,loginUid,"2");
        return Db.findFirst(sqlPara);
    }

    public List<Record> getStaticListByType(String type) {
        SqlPara sqlPara = Db.getSqlPara("adv.getStaticListByType",type);
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = Lists.newArrayList();
        }
        for (Record r : records) {
            doFilePath(r);

        }
        return records;
    }
    @Before(Tx.class)
    public Record endPlayMediaAdvById(String loginUid, String advId) {
        {
             AdvService advService=new AdvService();
            Record re = new Record();
            re.set("msg", "");

            Record r = advService.getAdvById(advId,loginUid);
            String cid = IdGen.uuid();
            String sqlPara = Db.getSql("user.historyAdd");
            //用户历史记录
            Db.update(sqlPara, cid, loginUid, r.getStr("imgSrc"), r.getStr("imgLinks"), r.getStr("imgNote"), r.getStr("imgRank"), r.getStr("model")
                    , advId, r.getStr("title"), r.getStr("type"), r.getStr("coverImgSrc"));




            return re;
        }
    }
}
